{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from train import *\n",
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"4\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib\n",
    "f= open('./Data/Processed/customer_feat.pkl','rb')\n",
    "customer_id = joblib.load(f)\n",
    "FN = joblib.load(f)\n",
    "Active = joblib.load(f)\n",
    "club_member_status = joblib.load(f)\n",
    "fashion_news_frequency = joblib.load(f)\n",
    "age = joblib.load(f)\n",
    "customer_cnt = joblib.load(f)\n",
    "customer_bidict = joblib.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model, feature_columns, behavior_feature_list = set_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.load_state_dict(torch.load(\"./Model/model2.pt\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_artid_seq_last = joblib.load('./Data/Processed/divide/pos_artid_seq_val.db')\n",
    "pos_pcode_seq_last = joblib.load('./Data/Processed/divide/pos_pcode_seq_val.db')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_feat = open('./Data/Processed/divide/pos_feat_val.db','rb')\n",
    "pos_custom_id_last = joblib.load(pos_feat)\n",
    "pos_FN_last = joblib.load(pos_feat)\n",
    "pos_Active_last = joblib.load(pos_feat)\n",
    "pos_club_member_status_last = joblib.load(pos_feat)\n",
    "pos_fashion_news_frequency_last = joblib.load(pos_feat)\n",
    "pos_age_last = joblib.load(pos_feat)\n",
    "pos_artid_last = joblib.load(pos_feat)\n",
    "pos_pcode_last = joblib.load(pos_feat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bidict import bidict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "pos_in_last = np.array([-1 for i in range(1371980)])\n",
    "for i in range(pos_custom_id_last.size):\n",
    "    pos_in_last[pos_custom_id_last[i]] = i\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "history = {}\n",
    "for i in customer_id:\n",
    "    if pos_in_last[i] != -1:\n",
    "        artid_seq_i = pos_artid_seq_last[pos_in_last[i]]\n",
    "        pcode_seq_i = pos_pcode_seq_last[pos_in_last[i]]\n",
    "        if artid_seq_i[-1] == 0:\n",
    "            for j in range(200):\n",
    "                if artid_seq_i[j] == 0:\n",
    "                    artid_seq_i[j] = pos_artid_last[pos_in_last[i]]\n",
    "                    break\n",
    "        else:\n",
    "            artid_seq_i = np.append(artid_seq_i[1:],pos_artid_last[pos_in_last[i]])\n",
    "            pcode_seq_i = np.append(pcode_seq_i[1:],pos_pcode_last[pos_in_last[i]])\n",
    "        history[i] = {'artid_seq':artid_seq_i,'pcode_seq':pcode_seq_i}\n",
    "    else:\n",
    "        artid_seq_i = np.array([0 for j in range(200)])\n",
    "        pcode_seq_i = np.array([0 for j in range(200)])\n",
    "        history[i] = {'artid_seq':artid_seq_i,'pcode_seq':pcode_seq_i}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "article_sparse = open('./Data/Processed/article_sparse.pkl','rb')\n",
    "artid = joblib.load(article_sparse)\n",
    "pcode = joblib.load(article_sparse)\n",
    "artid_bidict = joblib.load(article_sparse)\n",
    "pcode_bidict = joblib.load(article_sparse)\n",
    "artid_cnt = joblib.load(article_sparse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[      0       1       2 ... 1371977 1371978 1371979]\n",
      "0 w ok\n",
      "10000 w ok\n",
      "20000 w ok\n",
      "30000 w ok\n",
      "40000 w ok\n",
      "50000 w ok\n",
      "60000 w ok\n",
      "70000 w ok\n",
      "80000 w ok\n",
      "90000 w ok\n",
      "100000 w ok\n",
      "110000 w ok\n",
      "120000 w ok\n",
      "130000 w ok\n",
      "140000 w ok\n",
      "150000 w ok\n",
      "160000 w ok\n",
      "170000 w ok\n",
      "180000 w ok\n",
      "190000 w ok\n",
      "200000 w ok\n",
      "210000 w ok\n",
      "220000 w ok\n",
      "230000 w ok\n",
      "240000 w ok\n",
      "250000 w ok\n",
      "260000 w ok\n",
      "270000 w ok\n",
      "280000 w ok\n",
      "290000 w ok\n",
      "300000 w ok\n",
      "310000 w ok\n",
      "320000 w ok\n",
      "330000 w ok\n",
      "340000 w ok\n",
      "350000 w ok\n",
      "360000 w ok\n",
      "370000 w ok\n",
      "380000 w ok\n",
      "390000 w ok\n",
      "400000 w ok\n",
      "410000 w ok\n",
      "420000 w ok\n",
      "430000 w ok\n",
      "440000 w ok\n",
      "450000 w ok\n",
      "460000 w ok\n",
      "470000 w ok\n",
      "480000 w ok\n",
      "490000 w ok\n",
      "500000 w ok\n",
      "510000 w ok\n",
      "520000 w ok\n",
      "530000 w ok\n",
      "540000 w ok\n",
      "550000 w ok\n",
      "560000 w ok\n",
      "570000 w ok\n",
      "580000 w ok\n",
      "590000 w ok\n",
      "600000 w ok\n",
      "610000 w ok\n",
      "620000 w ok\n",
      "630000 w ok\n",
      "640000 w ok\n",
      "650000 w ok\n",
      "660000 w ok\n",
      "670000 w ok\n",
      "680000 w ok\n",
      "690000 w ok\n",
      "700000 w ok\n",
      "710000 w ok\n",
      "720000 w ok\n",
      "730000 w ok\n",
      "740000 w ok\n",
      "750000 w ok\n",
      "760000 w ok\n",
      "770000 w ok\n",
      "780000 w ok\n",
      "790000 w ok\n",
      "800000 w ok\n",
      "810000 w ok\n",
      "820000 w ok\n",
      "830000 w ok\n",
      "840000 w ok\n",
      "850000 w ok\n",
      "860000 w ok\n",
      "870000 w ok\n",
      "880000 w ok\n",
      "890000 w ok\n",
      "900000 w ok\n",
      "910000 w ok\n",
      "920000 w ok\n",
      "930000 w ok\n",
      "940000 w ok\n",
      "950000 w ok\n",
      "960000 w ok\n",
      "970000 w ok\n",
      "980000 w ok\n",
      "990000 w ok\n",
      "1000000 w ok\n",
      "1010000 w ok\n",
      "1020000 w ok\n",
      "1030000 w ok\n",
      "1040000 w ok\n",
      "1050000 w ok\n",
      "1060000 w ok\n",
      "1070000 w ok\n",
      "1080000 w ok\n",
      "1090000 w ok\n",
      "1100000 w ok\n",
      "1110000 w ok\n",
      "1120000 w ok\n",
      "1130000 w ok\n",
      "1140000 w ok\n",
      "1150000 w ok\n",
      "1160000 w ok\n",
      "1170000 w ok\n",
      "1180000 w ok\n",
      "1190000 w ok\n",
      "1200000 w ok\n",
      "1210000 w ok\n",
      "1220000 w ok\n",
      "1230000 w ok\n",
      "1240000 w ok\n",
      "1250000 w ok\n",
      "1260000 w ok\n",
      "1270000 w ok\n",
      "1280000 w ok\n",
      "1290000 w ok\n",
      "1300000 w ok\n",
      "1310000 w ok\n",
      "1320000 w ok\n",
      "1330000 w ok\n",
      "1340000 w ok\n",
      "1350000 w ok\n",
      "1360000 w ok\n",
      "1370000 w ok\n"
     ]
    }
   ],
   "source": [
    "article_pool = []\n",
    "topk_index_100 = open('./Data/Processed/topk_index_100.pkl','rb')\n",
    "topk_index = joblib.load(topk_index_100)\n",
    "defaultsample = np.random.choice(range(1, 105543), 100)\n",
    "print(customer_id)\n",
    "for i in customer_id:\n",
    "    if (i % 10000 == 0):\n",
    "        print(f'{i} w ok')\n",
    "    if history[i]['artid_seq'][0] == 0:\n",
    "        history[i]['artid_seq'][0] = np.random.randint(1,105543)\n",
    "    t = 0\n",
    "    for j in history[i]['artid_seq']:\n",
    "        if j != 0:\n",
    "            t = j\n",
    "        else:\n",
    "            article_pool.append(topk_index[t - 1])\n",
    "            break "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#result = []\n",
    "#batch = {name:np.array([]) for name in get_feature_names(feature_columns)}\n",
    "#article_pool[i] = article_pool[i].astype(int)\n",
    "#num = article_pool[i].size\n",
    "import csv\n",
    "Output = open('result.csv','w')\n",
    "writer = csv.writer(Output)\n",
    "header = ['article0','article1','article2','article3','article4','article5','article6','article7','article8','article9','article10','article11']\n",
    "\n",
    "for i in customer_id:\n",
    "    num = 100\n",
    "    batch = {}\n",
    "    if(i % 100000 == 0):\n",
    "        print(i)\n",
    "    batch['pos_custom_id'] = np.array([i]).repeat(num)\n",
    "    batch['pos_Active'] = np.array([Active[i]]).repeat(num)\n",
    "    batch['pos_FN'] = np.array([FN[i]]).repeat(num)\n",
    "    batch['pos_club_member_status'] = np.array([club_member_status[i]]).repeat(num)\n",
    "    batch['pos_fashion_news_frequency'] = np.array([fashion_news_frequency[i]]).repeat(num)\n",
    "    batch['pos_artid'] = article_pool[i]\n",
    "    batch['pos_pcode'] = pcode[batch['pos_artid']]\n",
    "    batch['pos_sales_channel_id'] = np.random.randint(2,size = num)\n",
    "    batch['pos_age'] = np.array([age[i] for j in range(num)])\n",
    "    batch['hist_pos_artid'] = np.array([history[i]['artid_seq'] for j in range(num)])\n",
    "    #seq_length = np.count_nonzero(history[i]['artid_seq'])\n",
    "    batch['seq_length'] = np.array([np.count_nonzero(history[i]['artid_seq'])]).repeat(num)\n",
    "    batch['hist_pos_pcode'] = np.array([history[i]['pcode_seq'] for j in range(num)])\n",
    "#Batch.append(batch)\n",
    "# if(i % 25 == 0):\n",
    "#     ctr = model.predict()\n",
    "    ctr = model.predict(batch, num)  \n",
    "    print(ctr) \n",
    "    result = article_pool[i][np.argsort(ctr)[-12:]]\n",
    "    print(result)\n",
    "    writer.writerow(result.tolist())     \n",
    "    #result.append(article_pool[i][np.argsort(ctr)[-12:]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'result' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_57376/2662801290.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0;31m#     y = np.array(ctr[i * 100, (i+1)*100])\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0;31m#     result.append(article_pool[i][np.argsort(y)[-12:]])\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'result' is not defined"
     ]
    }
   ],
   "source": [
    "# ctr = []\n",
    "# cut = []\n",
    "# for i in range(customer_id.size):\n",
    "#     if(i % 30 == 0):\n",
    "#         cut.append(i)\n",
    "# cut.append(customer_id[-1])\n",
    "# for i in range(len(cut) - 1):\n",
    "#     x = {name:batch[name][cut[i]*100,cut[i+1]*100] for name in get_feature_names(feature_columns)}\n",
    "#     tmp = model.predict(x)\n",
    "#     ctr = ctr + tmp.tolist()\n",
    "# for i in customer_id:\n",
    "#     y = np.array(ctr[i * 100, (i+1)*100])\n",
    "#     result.append(article_pool[i][np.argsort(y)[-12:]])\n",
    "result = np.array(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "jlout = open('result.db','wb')\n",
    "joblib.dump(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "Output = open('result.csv','w')\n",
    "writer = csv.writer(Output)\n",
    "header = ['customer_id','article0','article1','article2','article3','article4','article5','article6','article7','article8','article9','article10','article11']\n",
    "for i in customer_id:\n",
    "    writer.writerow(['{customer_bidict.inverse[i]}',result[i][j] for j in range(12)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "7c2e65e64076883662e9fbb467097aa6d81839e6b77012bdd0b6d8c4fbfb9623"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 ('SR-GNN')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
